﻿using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Printing;
using System.Windows;
using System.Collections.Generic;
using System.Windows.Shapes;
using System.Windows.Media;

namespace MyFloridaHouse.libSvMassDataReport.Reports
{
    public class ACSDistrictSummaryPrint
    {
        public void Print(ObservableCollection<ACSDistrictSummarySection> sectionList)
        {
            PrintDocument pd = new PrintDocument();
            int sectionItemIndex = 0;
            int sectionItemRowIndex = 0;
            bool buildingHeader = true;
            ObservableCollection<ACSDistrictSummaryReportRow> currentRowList = new ObservableCollection<ACSDistrictSummaryReportRow>();
            int currentRowIndex = 0;
            pd.PrintPage += (s, pe) =>
            {
                StackPanel itemHost = new StackPanel();
                double areaLeftOnPage = pe.PrintableArea.Height;
                bool doBreak = false;
                while (sectionItemIndex < sectionList.Count)
                {
                    if (doBreak)
                    {
                        break;
                    }
                    if (buildingHeader)
                    {
                        ACSDistrictSummarySectionLayoutHeader itemContainer = new ACSDistrictSummarySectionLayoutHeader();
                        itemContainer.DataContext = sectionList[sectionItemIndex];
                        itemHost.Children.Add(itemContainer);
                        itemHost.Measure(new Size(pe.PrintableArea.Width, double.PositiveInfinity));
                        if (itemContainer.headBorder.DesiredSize.Height > areaLeftOnPage && itemHost.Children.Count > 1)
                        {
                            itemHost.Children.Remove(itemContainer);
                            pe.HasMorePages = true;
                            break;
                        }
                        areaLeftOnPage = pe.PrintableArea.Height - itemHost.DesiredSize.Height;
                        buildingHeader = false;
                        currentRowList.Clear();
                        sectionItemRowIndex = 0;
                    }
                    else //building data rows
                    {
                        currentRowList.Clear();
                        currentRowIndex = 0;
                        ACSDistrictSummarySectionLayoutRowItems itemRowContainer = new ACSDistrictSummarySectionLayoutRowItems();
                        itemRowContainer.rowsDataGrid.ItemsSource = currentRowList;
                        itemHost.Children.Add(itemRowContainer);
                        while (sectionItemRowIndex < sectionList[sectionItemIndex].dataList.Count)
                        {
                            currentRowList.Add(sectionList[sectionItemIndex].dataList[currentRowIndex]);
                            if (areaLeftOnPage <= 0)
                            {
                                currentRowList.RemoveAt(currentRowIndex);
                                pe.HasMorePages = true;
                                
                                doBreak = true;
                                break;
                            }
                            currentRowIndex += 1;
                            sectionItemRowIndex += 1;
                            areaLeftOnPage = areaLeftOnPage - 25.00;
                        }
                        if (!doBreak)
                        {
                            sectionItemIndex += 1;
                            buildingHeader = true;
                        }   
                        
                    }
                }
                pe.PageVisual = itemHost;
            };
            pd.Print("test print");
        }
    }
}
